package com.century.sjt.activity.Topic;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.bumptech.glide.Glide;
import com.century.sjt.R;
import com.century.sjt.EMChat.activity.ChatActivity;
import com.century.sjt.activity.BaseActivity;
import com.century.sjt.entity.ItemBean;
import com.century.sjt.util.Constant;
import com.century.sjt.util.TipUtil;
import com.century.sjt.widget.TitleBar.SjtTitleBar;
import com.makeramen.roundedimageview.RoundedImageView;

import org.json.JSONObject;
import org.json.JSONTokener;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by 炳基 on 2016/6/20.
 */
public class MyTopActivity extends BaseActivity {
    private SjtTitleBar titleBar;
    private String userName;
    private Button btnChatRoom;
    private RoundedImageView Avatar;
    private TextView tvName;
    private TextView tvNick;
    private TextView tvRemark;
    private TextView tvPhome;
    private TextView tvComment;
    private LinearLayout layoutSetNick;
    private LinearLayout layoutToHome;
    private EditText edSetNick;
    private Button btnSetDiss, btnSetSuccee;

    private TextView tvWhatsUp;

    private Context mCpntext;
    private String imageId="";

    private ImageView img1,img2,img3,img4,img5;
    private ImageView lastTopic_1,lastTopic_2,lastTopic_3;
    private List<ImageView> imgList = new ArrayList<>();
    private List<ImageView> lastTopic = new ArrayList<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mytop_activity);
        mCpntext = this;
        //获取用户ID
        userName = getIntent().getExtras().getString("username");
        initViews();
        initEvents();
    }

    private void initViews() {

        titleBar = (SjtTitleBar) findViewById(R.id.title_bar);
        //       标题栏
        titleBar.setTitle(getResources().getString(R.string.tab_new_mytop_details));
        titleBar.setLeftLayoutClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        btnChatRoom = (Button) findViewById(R.id.btn_to_chatroom);
        Avatar = (RoundedImageView) findViewById(R.id.mytop_touxiang_image);
        tvComment = (TextView) findViewById(R.id.mytop_comment);
        tvNick = (TextView) findViewById(R.id.mytop_nick);
        tvRemark = (TextView) findViewById(R.id.tv_remark);
        tvPhome = (TextView) findViewById(R.id.mytop_phome);
        layoutSetNick = (LinearLayout) findViewById(R.id.mytop_setlayout_nick);
        layoutToHome = (LinearLayout) findViewById(R.id.mytop_layout_tohome);

        tvWhatsUp = (TextView) findViewById(R.id.tv_whatsup);

        img1 = (ImageView) findViewById(R.id.img_1);
        img2 = (ImageView) findViewById(R.id.img_2);
        img3 = (ImageView) findViewById(R.id.img_3);
        img4 = (ImageView) findViewById(R.id.img_4);
        img5 = (ImageView) findViewById(R.id.img_5);

        lastTopic_1 = (ImageView) findViewById(R.id.lastTopic_1);
        lastTopic_2 = (ImageView) findViewById(R.id.lastTopic_2);
        lastTopic_3 = (ImageView) findViewById(R.id.lastTopic_3);


        imgList.add(img1);
        imgList.add(img2);
        imgList.add(img3);
        imgList.add(img4);
        imgList.add(img5);

        lastTopic.add(lastTopic_1);
        lastTopic.add(lastTopic_2);
        lastTopic.add(lastTopic_3);
    }

    private void initEvents() {
        btnChatRoom.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {//进入聊天页面
                startActivity(new Intent(MyTopActivity.this, ChatActivity.class).putExtra("userId", userName));
            }
        });
        layoutToHome.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(MyTopActivity.this, HomepageActivity.class).putExtra("userId", userName));
            }
        });
        //修改备注
        layoutSetNick.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(MyTopActivity.this, RemarkActivity.class).putExtra("userId", userName).putExtra("remark", tvRemark.getText().toString().trim()));
            }
        });
        if (null == userName || "".equals(userName)) {

        } else {
//            EaseUserUtils.setUserMobile(userName, tvPhome);
//            EaseUserUtils.setUserNick(userName, tvNick);
//            EaseUserUtils.setUserAvatar(this, userName, Avatar);
        }
        Avatar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MyTopActivity.this, ImageShowActivity.class);
                intent.putExtra("imgid", imageId);
                startActivity(intent);
            }
        });

    }

    @Override
    protected void onResume() {
        super.onResume();
        friendDetail();

    }

    @Override
    protected void initView() {

    }

    @Override
    protected void initEvent() {

    }

    @Override
    protected void initData() {

    }

    /**
     * 好友详情
     */
    private void friendDetail() {
        //显示Progress Dialog
        TipUtil.openProgressDialog(Constant.APPLICATION_NAME, getResources().getString(R.string.error_please_waitting), MyTopActivity.this);
        StringRequest stringRequest = new StringRequest(Request.Method.POST, Constant.FriendDetailHost, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                try {
                    TipUtil.closeProgressDialog();
                    TipUtil.log("好友详情", response.toString(), 1);
                    JSONTokener jsonParser = new JSONTokener(response);
                    JSONObject res = (JSONObject) jsonParser.nextValue();
                    String code = res.getString("code");
                    String message = res.getString("message");
                    if (code.equals("407")){
                        TipUtil.showAppayRes(mCpntext);
                    }
                    if (code.equals("200")) {//成功
                        JSONObject data = res.getJSONObject("data");
                        String friendRemark = data.getString("remark");
                        String nickName = data.getString("nickName");
                        String mobile = data.getString("mobile");
                        String whatsUp = data.getString("whatsUp");
                        String shopId = data.getString("shopId");
                        String isShop = data.getString("isShop");
                        String mobileCode = data.getString("mobileCode");
                        String lastTopic = data.getString("lastTopic");
                        imageId = data.getString("imageId");
                        tvRemark.setText(friendRemark);
                        tvNick.setText(nickName);
                        tvPhome.setText("+("+ mobileCode +")"+mobile);
                        tvWhatsUp.setText(whatsUp);
//                        Log.e("img",Constant.ImageHost_Small + imageId);
                        String imgUrl = Constant.ImageHost_Small + imageId;
                        Glide.with(MyTopActivity.this).load(imgUrl).into(Avatar);
                        int level = data.getInt("level");
                        if ("0".equals(isShop)){
                            if (level>5){
                                for (int i=0;i<5;i++){
                                    imgList.get(i).setVisibility(View.VISIBLE);
                                    imgList.get(i).setImageDrawable(getResources().getDrawable(R.mipmap.starorange2x));
                                    //闪烁
                                    AlphaAnimation alphaAnimation1 = new AlphaAnimation(0.1f, 1.0f);
                                    alphaAnimation1.setDuration(1000);
                                    alphaAnimation1.setRepeatCount(Animation.INFINITE);
                                    alphaAnimation1.setRepeatMode(Animation.REVERSE);
                                    imgList.get(i).setAnimation(alphaAnimation1);
                                    alphaAnimation1.start();
                                }
                            }else {
                                for (int i=0;i<level;i++){
                                    imgList.get(i).setVisibility(View.VISIBLE);
                                    imgList.get(i).setImageDrawable(getResources().getDrawable(R.mipmap.starorange2x));
                                }
                            }

                        }else {
                            if (level>5){
                                for (int i=0;i<5;i++){
                                    imgList.get(i).setVisibility(View.VISIBLE);
                                    imgList.get(i).setImageDrawable(getResources().getDrawable(R.mipmap.diamondorange2x));
                                    //闪烁
                                    AlphaAnimation alphaAnimation1 = new AlphaAnimation(0.1f, 1.0f);
                                    alphaAnimation1.setDuration(1000);
                                    alphaAnimation1.setRepeatCount(Animation.INFINITE);
                                    alphaAnimation1.setRepeatMode(Animation.REVERSE);
                                    imgList.get(i).setAnimation(alphaAnimation1);
                                    alphaAnimation1.start();
                                }
                            }else {
                                for (int i=0;i<level;i++){
                                    imgList.get(i).setVisibility(View.VISIBLE);
                                    imgList.get(i).setImageDrawable(getResources().getDrawable(R.mipmap.diamondorange2x));
                                }
                            }

                        }
                        mShowImage(lastTopic);

                    } else {//失败
                        TipUtil.showToast(message, MyTopActivity.this, 1);
                    }
                } catch (Exception ex) {
                    ex.printStackTrace();
                    TipUtil.showToast(getResources().getString(R.string.error_service), MyTopActivity.this, 1);
                }

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                TipUtil.showToast(getResources().getString(R.string.error_network), MyTopActivity.this, 1);
                TipUtil.closeProgressDialog();
            }
        }) {
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String, String> map = new HashMap<String, String>();
                SharedPreferences mainSharedPreferences = getSharedPreferences(Constant.TAG, Activity.MODE_PRIVATE);
                String userId = mainSharedPreferences.getString("userId", "");
                map.put("loginId", userId);
                map.put("friendId", userName);
                return map;
            }

            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                SharedPreferences mainSharedPreferences = getSharedPreferences(Constant.TAG, Activity.MODE_PRIVATE);
                String cookie = mainSharedPreferences.getString("cookie", "");
                String rememberMeValue = mainSharedPreferences.getString("rememberMe", "");
                HashMap localHashMap = new HashMap();
                String cookieValue = cookie + ";" + rememberMeValue;
                localHashMap.put("Cookie", cookieValue);
                return localHashMap;
            }
        };
        mQueue.add(stringRequest);
    }
    private void mShowImage(String imgURL){
        String [] newsplitstr = imgURL.split("\\|");
        final String[] imageUrls = new String[newsplitstr.length];

        for (int i=0;i<newsplitstr.length;i++){
            String aimgUrl = Constant.BaseImageUrl + newsplitstr[i];
//            Log.e("url",aimgUrl);
            ImageView ShowView = lastTopic.get(i);
            ShowView.setVisibility(View.VISIBLE);
            Glide.with(MyTopActivity.this).load(aimgUrl).into(ShowView);
        }

    }
}
